SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
CREATE PROCEDURE [dbo].[SP_TMDOCU_PROV_I04]        
/*-------------------------------------------------------*/        
/*---Empresa              : OFISIS S.A.                --*/        
/*---Cliente              : OFISIS S.A.                --*/        
/*---Sistema              : Tesoreria                  --*/        
/*---M®dulo               : Empresa                    --*/        
/*---Programa             : Actualizacion TDDOCU_ALMA  --*/         
/*---                       Logistica         --*/        
/*---Script               :           --*/        
/*---Nombre SP            : SP_TMDOCU_PROV_I04         --*/        
/*---Desarrollado por     : Javier Flores A.           --*/        
/*---Fecha Creaci®n       : 05/02/2002                 --*/        
/*---Base Datos           : Microsoft Sql Server       --*/        
/*---Versi®n              :                         --*/        
/*---Invoca a SP          :                            --*/        
/*-------------------------------------------------------*/        
        
/*-------------------------------------------------------*/        
/*---Modificado 1 por     :                            --*/        
/*---Fecha Modificaci®n   :                            --*/         
/*---Detalle Modificaci®n :                            --*/        
/*-------------------------------------------------------*/        
        
/*-------------------------------------------------------*/        
/*---Comentarios                                       --*/        
/*---                                                  --*/        
/*---                                                  --*/        
/*-------------------------------------------------------*/        
        
/*-------------------------------------------------------*/        
/*---Drop Proc SP_TMDOCU_PROV_I04                      --*/        
/*---Grant all on SP_TMDOCU_PROV_I04 to public         --*/         
/*---SP_TMDOCU_PROV_I04 '01', 'OFISIS', '001', 'EXT00000031', 'IOC', '00000351', 3.485, '10557546', 3, '001'  
--*/        
/*-------------------------------------------------------*/        
 @ISCO_EMPR   TD_VC_002,         
 @ISCO_USUA_MODI TD_VC_008_USUA,        
 @ISCO_UNID   TD_VC_003,        
 @ISCO_PROV   TD_VC_020,        
 @ISCO_TIPO_DOCU TD_VC_003,        
 @ISNU_DOCU_PROV TD_VC_020,        
 @INFA_TIPO_CAMB TD_NU_016_004,        
 @ISCO_ITEM   TD_VC_020,        
 @INIM_COST_COMP TD_NU_016_004,        
 @ISCO_ALMA      TD_VC_003,        
 @INNU_SECU      TD_IN_001        
 as        
        
 DECLARE        
 @VSCO_MONE       TD_VC_003,        
 @CSCO_MONE       TD_VC_003,         
 @CNCA_DOCU_MOVI  TD_NU_016_004,         
 @CNCA_DOCU_ALMA  TD_NU_016_004,        
 @VNFA_CONV       TD_NU_016_004,          
 @VNIM_COST_MOVI  TD_NU_016_004,        
 @VNIM_COST_ALMA  TD_NU_016_004        
        
 CREATE TABLE #TWDOCU_ALMA_1        
 (CO_MONE       CHAR(3) NULL,        
  CA_DOCU_MOVI  NUMERIC(16,4) NULL,        
  CA_DOCU_ALMA  NUMERIC(16,4) NULL)        
        
 Begin Tran  SP_TMDOCU_PROV_I04        
         
   SELECT @VSCO_MONE = CO_MONE_DEFA        
   FROM TMPARA_TESO        
   WHERE CO_EMPR = @ISCO_EMPR        
        
IF  NOT EXISTS ( SELECT T1.NU_DOCU        
   FROM TCDOCU_ALMA T1, TTCIER_MENS T2        
   WHERE T1.NU_DOCU = @ISNU_DOCU_PROV        
   And   T1.TI_DOCU = @ISCO_TIPO_DOCU        
   AND   T1.CO_UNID = @ISCO_UNID        
   And   T1.CO_EMPR = @ISCO_EMPR        
   And   T1.TI_AUXI_EMPR = 'P'        
   And   T1.CO_AUXI_EMPR = @ISCO_PROV        
   AND   T1.CO_ALMA = T2.CO_ALMA         
   AND   T1.CO_UNID = T2.CO_UNID        
   AND   T1.CO_EMPR = T2.CO_EMPR        
   AND   T2.AA_CIER = YEAR(T1.FE_DOCU)        
   AND   T2.MM_CIER = MONTH(T1.FE_DOCU)           
   )        
  BEGIN             
        
          INSERT INTO #TWDOCU_ALMA_1 (CO_MONE, CA_DOCU_MOVI, CA_DOCU_ALMA )                 
          Select CO_MONE, CA_DOCU_MOVI, CA_DOCU_ALMA     
          From  TDDOCU_ALMA                           
   Where NU_DOCU = @ISNU_DOCU_PROV        
   And TI_DOCU = @ISCO_TIPO_DOCU        
          And   NU_SECU = @INNU_SECU        
          And   CO_ALMA = @ISCO_ALMA        
   And CO_UNID = @ISCO_UNID        
    And   CO_EMPR = @ISCO_EMPR        
   And TI_AUXI_EMPR = 'P'        
   And CO_AUXI_EMPR = @ISCO_PROV        
   And CO_ITEM = @ISCO_ITEM        
        
          Declare CU_TDDOCU_ALMA_I01_1 Cursor         
      For        
          Select CO_MONE, CA_DOCU_MOVI, CA_DOCU_ALMA        
          From  #TWDOCU_ALMA_1                           
        
          Open CU_TDDOCU_ALMA_I01_1         
          Fetch CU_TDDOCU_ALMA_I01_1 into @CSCO_MONE, @CNCA_DOCU_MOVI, @CNCA_DOCU_ALMA    
        
          While (@@FETCH_STATUS = 0)        
         Begin        
                If (@@FETCH_STATUS < 0)        
             Begin        
             RAISERROR 20001 'FIN DE ARCHIVO'        
                    Close CU_TDDOCU_ALMA_I01_1        
             Deallocate CU_TDDOCU_ALMA_I01_1        
                    Rollback tran        
             Return        
               End        
        
                SELECT @VNFA_CONV = PATINDEX('1', LTRIM(RTRIM(STR(PATINDEX( @VSCO_MONE, @CSCO_MONE))))) +         
                 PATINDEX('0', LTRIM(RTRIM(STR(PATINDEX( @VSCO_MONE, @CSCO_MONE))))) *  @INFA_TIPO_CAMB        
                SELECT @VNIM_COST_MOVI = ROUND( ( ( @INIM_COST_COMP * @CNCA_DOCU_MOVI ) / @CNCA_DOCU_ALMA ), 4 )        
                SELECT @VNIM_COST_ALMA = ( ROUND( ( ( @INIM_COST_COMP * @CNCA_DOCU_MOVI ) / @CNCA_DOCU_ALMA ), 4 ) ) *        
 
                (PATINDEX('1', LTRIM(RTRIM(STR(PATINDEX( @VSCO_MONE, @CSCO_MONE))))) +         
      PATINDEX('0', LTRIM(RTRIM(STR(PATINDEX( @VSCO_MONE, @CSCO_MONE))))) *  @INFA_TIPO_CAMB)        
        
  Update  TDDOCU_ALMA        
     Set  FA_CAMB = @INFA_TIPO_CAMB,        
          --  FA_CONV = @VNFA_CONV,        
      IM_COST_COMP = @INIM_COST_COMP,        
    --  IM_COST_MOVI = @VNIM_COST_MOVI,        
        IM_COST_ALMA = @VNIM_COST_ALMA,        
           IM_COTO_ALMA = @VNIM_COST_ALMA * @CNCA_DOCU_ALMA,      
      CO_USUA_MODI = @ISCO_USUA_MODI        
  Where NU_DOCU = @ISNU_DOCU_PROV        
  And TI_DOCU = @ISCO_TIPO_DOCU        
  And     NU_SECU = @INNU_SECU        
  And     CO_ALMA = @ISCO_ALMA        
  And CO_UNID = @ISCO_UNID        
  And     CO_EMPR = @ISCO_EMPR        
  And TI_AUXI_EMPR = 'P'        
  And CO_AUXI_EMPR = @ISCO_PROV        
  And CO_ITEM = @ISCO_ITEM        
        
     If @@error <> 0        
        Begin        
             Rollback tran        
             Return        
        End        
        
                Fetch CU_TDDOCU_ALMA_I01_1 into @CSCO_MONE, @CNCA_DOCU_MOVI, @CNCA_DOCU_ALMA        
             End        
        
        Close CU_TDDOCU_ALMA_I01_1        
        Deallocate CU_TDDOCU_ALMA_I01_1        
  END        
        
Commit        
/*******************************FIN******************************/


GO
